package br.com.livraria.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.livraria.business.UsuarioBo;
import br.com.livraria.exception.ValidationException;
import br.com.livraria.util.Constantes;
import br.com.livraria.vo.Usuario;

/**
 * Servlet implementation class LivroServlet
 */
@WebServlet("/usuario")
public class UsuarioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private String operacao = "";
	private UsuarioBo usuarioBo = new UsuarioBo();
	
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UsuarioServlet() {
        super();
    }

    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processar(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processar(request, response);
	}
    
	private void processar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		operacao = request.getParameter("operacao");
		
		
		if(operacao !=null){
			if(Constantes.CADASTRAR.equals(operacao)){
				cadastrar(request, response);
			} else if(Constantes.ALTERARSENHA.equals(operacao)){
				alterarSenha(request, response);
			} else if(Constantes.CONSULTAR_POR_ID.equals(operacao)){
				consultarPorId(request, response);
			}
		} else {
			//TODO fazer apearecer erro
		}
       	
 		
	}
    

	private void cadastrar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    	
	    	boolean temErro = false;
	    	RequestDispatcher rd = null;
	    	
	    	String email = request.getParameter("email");
	    	String senha = request.getParameter("senha");
	    	String nome = request.getParameter("nome");
	    	Integer telefone = Integer.parseInt(request.getParameter(("telefone")));
	    	Usuario usuario = new Usuario();
	    	
	    	 usuario.setEmail(email);
			usuario.setSenha(senha);
			usuario.setNome(nome);
			usuario.setTelefone(telefone);
	  
	    	if(!temErro) {
	    		request.setAttribute("msg", "O email " + usuario.getEmail() + " foi cadastrado com sucesso!");
	    	}
	    	
	    	rd = getServletContext().getRequestDispatcher("/cadastroUsuario.jsp");
			rd.forward(request, response);
	  
	    }

	   
	    private void alterarSenha(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    	boolean temErro = false;
	    	RequestDispatcher rd = null;
	    	
	    	String senha = request.getParameter("senha");
	    	Integer id = Integer.parseInt(request.getParameter("id"));
	    	
	    	Usuario u = new Usuario();
	    	u.setId(id);
	    	u.setSenha(senha);
	    	
	    	
	    	try {
				usuarioBo.alterarSenha(u);
			} catch (ValidationException e) {
				temErro = true;
				request.setAttribute("erro", e.getMensagem());
			}
	  
	    	if(!temErro) {
	    		request.setAttribute("msg", "senha " + u.getNome() + " Alterada com sucesso!");
	    	}
	    		    	
   			rd.forward(request, response);
	    }
	    
	    private void consultarPorId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	    }
	    
}
